package com.presto.directo.utils

import android.content.Context
import androidx.core.os.bundleOf
import com.appsflyer.AppsFlyerLib
import com.facebook.appevents.AppEventsLogger
import com.presto.directo.base.XApplication
import com.presto.directo.net.getHttpMap

object Af {
    const val KEY = "4G28ukQnxPtryXwZrYtBYC"

    const val show_loan_page = "show_loan_page"
    const val click_loan = "click_loan"

    const val show_phone_login_page = "show_phone_login_page"
    const val click_get_verify_code = "click_get_verify_code"

    const val show_sms_login_page = "show_sms_login_page"
    const val sms_login_auto = "sms_login_auto"
    const val sms_login_auto_return = "sms_login_auto_return"

    const val show_personInfo_page = "show_personInfo_page"
    const val click_submit_personInfo = "click_submit_personInfo"
    const val success_submit_personInfo = "success_submit_personInfo"

    const val show_address_page = "show_address_page"
    const val click_submit_addressInfo = "click_submit_addressInfo"
    const val success_submit_addressInfo = "success_submit_addressInfo"

    const val show_work_page = "show_work_page"
    const val click_submit_workInfo = "click_submit_workInfo"
    const val success_submit_workInfo = "success_submit_workInfo"

    const val show_question_page = "show_question_page"
    const val click_submit_questionInfo = "click_submit_questionInfo"
    const val success_submit_questionInfo = "success_submit_questionInfo"

    const val show_contact_page = "show_contact_page"
    const val click_submit_contactInfo = "click_submit_contactInfo"
    const val success_submit_contactInfo = "success_submit_contactInfo"

    const val show_card_list_page = "show_card_list_page"
    const val show_add_card_page = "show_add_card_page"
    const val show_bank_list_page = "show_bank_list_page"
    const val click_add_card = "click_add_card"
    const val success_add_card = "success_add_card"

    const val click_select_card_next = "click_select_card_next"

    const val show_loan_confirm_page = "show_loan_confirm_page"
    const val success_get_amounts = "success_get_amounts"

    const val click_loan_confirm = "click_loan_confirm"
    const val success_loan_confirm = "success_loan_confirm"

    const val click_repay = "click_repay"
    const val show_repay_page = "show_repay_page"
    const val show_record_list_page = "show_record_list_page"

    fun getAfId(): String {
        if (KEY.isEmpty()) return ""
        return AppsFlyerLib.getInstance().getAppsFlyerUID(XApplication.application) ?: ""
    }
}

fun String.track(context: Context) {
    if (Af.KEY.isEmpty()) return
    val map = getHttpMap()
    val bundle = bundleOf()
    map.forEach {
        bundle.putString(it.key, it.value.toString())
    }
    AppsFlyerLib.getInstance().logEvent(context, this, map)
    AppEventsLogger.newLogger(context).logEvent(this, bundle)
}
